package com.jida.tijian.service;

import com.jida.tijian.domain.Users;
import com.jida.tijian.dto.ResponseObject;
import com.jida.tijian.exception.TijianAPPException;
import com.jida.tijian.exception.TijianAPPExceptionEnum;
import com.jida.tijian.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.stereotype.Service;

/**
 * 用户模块的业务层代码
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    UsersMapper usersMapper;

    /**
     * 处理登录请求
     */
   /* @Override
    public ResponseObject<Users> login(Users user){
        ResponseObject<Users> responseObject=null;
        //1.先查
        Users result=usersMapper.selectByPrimaryKey(user.getUserId());


        //2.看有没有
        if(result==null){
            //告诉前端
            responseObject=new ResponseObject<>(201,"手机号尚未注册");
            //return
            return responseObject;
        }

        //3.比对密码
        if(!result.getPassword().equals(user.getPassword())){
            //告诉前端
            responseObject=new ResponseObject<>(202,"密码错误");
            //return
            return responseObject;
        }

        responseObject=new ResponseObject<>(ResponseObject.SUCCESS_STATUS,ResponseObject.SUCCESS_DESC,result);

        return responseObject;
    }*/
    @Override
    public ResponseObject<Users> login(Users user){
        //int a=10/0;
        ResponseObject<Users> responseObject=null;
        //1.先查
        Users result=usersMapper.selectByPrimaryKey(user.getUserId());


        //2.看有没有
        if(result==null){
            //告诉前端
            //responseObject=new ResponseObject<>(201,"手机号尚未注册");
            throw new TijianAPPException(TijianAPPExceptionEnum.MOBILE_NOT_REGISTER.getCode(),TijianAPPExceptionEnum.MOBILE_NOT_REGISTER.getMessage());
        }

        //3.比对密码
        if(!result.getPassword().equals(user.getPassword())){
            //告诉前端
            throw new TijianAPPException(TijianAPPExceptionEnum.USERID_OR_PASSWORD_IS_WRONG.getCode(),TijianAPPExceptionEnum.USERID_OR_PASSWORD_IS_WRONG.getMessage());

        }

        result.setPassword("");
        responseObject=new ResponseObject<>(ResponseObject.SUCCESS_STATUS,ResponseObject.SUCCESS_DESC,result);

        return responseObject;
    }
}
